package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;

public class ReadFile {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(readFileByCharacter("d:/test.txt"));
		copyFileByByte("d:/test.docx");
		System.out.println("copy success");
		

	}
	private static String readFileByCharacter(String fileName){
		StringBuffer content = new StringBuffer();
		try{
			File file = new File(fileName);
			FileReader reader = new FileReader(file);
			BufferedReader bufferedReader = new BufferedReader(reader);
			String line = "";
			while((line = bufferedReader.readLine()) != null){
				content.append(line);
			}
			FileWriter writer = new FileWriter("d:/test1.txt");
			BufferedWriter bufferedWriter = new BufferedWriter(writer);
			bufferedWriter.write(content.toString());
			bufferedWriter.flush();
			bufferedWriter.close();
			writer.close();
			bufferedReader.close();
			reader.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return content.toString();
	}
	private static void copyFileByByte(String fileName){
		try{
			File file = new File(fileName);
			InputStream inputStream = new FileInputStream(file);
			BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
			File outputFile = new File("d:/test1.docx");
			OutputStream outputStream = new FileOutputStream(outputFile);
			BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
			
			byte b[] = new byte[1024];
			int i = 0;
			while( (i = bufferedInputStream.read(b)) != -1){
				bufferedOutputStream.write(b, 0, i);
			}
			bufferedOutputStream.flush();
			bufferedOutputStream.close();
			outputStream.close();
			bufferedInputStream.close();
			inputStream.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
	}
}
